// JavaScript Document
path="http://www.orkut.com/Application.aspx";
servrpath="http://orkut.familybuilder.com/";

//langv=document.getElementById('langv').value;
//alert(langv);
arrcomm=new Array();
arrcomm[0]=new Array('please enter other details','Please enter comments','please enter family story','please enter gift details','Please enter instructions','please enter name','Please enter location','Please enter address');
arrcomm[1]=new Array('por favor entrar en otros detalles','Por favor, introduzca comentarios','Por favor, introduzca historia familiar','Por favor, introduzca los detalles de regalo','Por favor, escriba las instrucciones','Por favor, introduzca el nombre','Por favor, introduzca la ubicación','Por favor, introduzca la dirección');

//debug function for testing purposes
function debug(msg){
		debugwindow=window.open('','debug','toolbar=1,resizable=1,width=500,height=500,scrollbars=yes');
		debugwindow.document.writeln(msg+'<br>');
	}

function refreshWindow()
{
	document.frm.submit();
}
function getloc(name)
{

window.top.location=path+'?appId=87222226184&up_chk=1&up_tab='+name;
}

function familyList1(user,apid){
	debug("familyList1");

url=servrpath+"FamilyList1.php?pid="+user+"&apid="+apid;
	
var params = {};


			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;


			makeCachedRequest(url, function(responseText){
		document.getElementById('divFamilyList1').innerHTML=responseText.text;
/* Sortable.create("boxes",
     {tag:'li',overlap:'horizontal',constraint: false,ghosting:false,
      onUpdate:function(){
	  url1=servrpath+'order.php?'+Sortable.serialize('boxes');
var params = {};


			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;


			makeCachedRequest( url1, function(responseText){function(responseText){
	 
	  }
		}, params, 0) ;
	 
        p = $("boxes");
        p.moves = p.moves || 0;
        p.moves++;
        $('puzzleinfo').innerHTML = 
          "You've made " + p.moves + " move" + (p.moves>1 ? "s" : "");
       Sortable.serialize("boxes");
		/*if(Sortable.serialize("boxes")==
           "boxes[]=1&boxes[]=2&boxes[]=3&boxes[]=4") {
           $('puzzleinfo').innerHTML = 
             "You've solved the puzzle in " + p.moves + " moves!";
           Element.Class.add('puzzleinfo','congrats');
        }
      },
	  onChange:function(element){$('testlist2_serialize').innerHTML = Sortable.serialize(element.parentNode)}
    }) */

				}, params, 0) ;
}

function getMyFriends1(pg){
	debug("getMyFriends1");
var req = opensocial.newDataRequest();

req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
req.add(req.newFetchPeopleRequest ('VIEWER_FRIENDS'), 'viewerFriends');
req.send(function(data){
var console = {};
console.log = function(msg) {};
person = data.get('viewer').getData();
my_data = data.get('viewerFriends');
my_data = my_data.getData() || {};
var totalFriendCount = data.get('viewerFriends').getData().getTotalSize();
//alert(totalFriendCount);
var mod=totalFriendCount%50;
//alert(mod);

var page;
if(mod==0)
{

 page=Math.floor(totalFriendCount/50);
 
}
else
{

 page=Math.floor(totalFriendCount/50);
 page=page+1;
 
}


	var params = {};
params[opensocial.DataRequest.PeopleRequestFields.MAX] = totalFriendCount;
//params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;
params[opensocial.DataRequest.PeopleRequestFields.SORT_ORDER] = opensocial.DataRequest.SortOrder.NAME;

var req = opensocial.newDataRequest();
req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS, params), 'viewerFriends');
req.send(function fetchPeopleHandler(resp)
{
//alert('ggg');
//alert(pg);
//alert(totalFriendCount);

if (!resp.hadError()) { // always verify whether your request was successful
    
	var viewerFriends = resp.get('viewerFriends').getData(); // use the key passed with the request to "get" the appropriate data


    var html;
	var j=0;
	html = '<table width="65%" cellspacing="2" cellpadding="2" align="center" border="0">';
	//var viewerFriends = dataResponse.get('viewerFriends').getData();
	if(viewerFriends){
	k=1;
	val=pg;
	limit=val*50;
	limitstart=limit-50;
	viewerFriends.each(function(person) {
	if(j>=limitstart && j<=limit){
	if(k%51!=0){
	k++;
profile= person.getField(opensocial.Person.Field.PROFILE_URL);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		
		html += '<tr><td style="height: 15px;"></td></tr><tr><td align="center"><div id="div'+j+'" style="background: #ffffff; border: 1px #dddddd solid ; cursor: pointer;" onclick="this.id.background=\'#769DD0\';addtofamilylist(\''+person.getId()+'\',\''+person.getDisplayName()+'\',\''+userPic+'\',\''+person.getField(opensocial.Person.Field.PROFILE_URL)+'\');"><table border="0" width="100%"><tr><td width="30%" style="text-align: center; " align="center"><img src="'+userPic+'" width="50px" height="50px" border="0"></td><td width="70%" style="text-align: left; font-family:tahoma;font-size:11px;color:#1E3A73; height:12px;">'+ person.getDisplayName() +'</td></tr></table></div></td></tr>';
    	
	}
	
	}
		j++;
	});
	
	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table><tr>';
	var i;
	for(i=1;i<=page;i++)
    {
	if(i==pg)
	{
	html += '<td><a href="#" onclick="getMyFriends1('+i+')" style="color:#FF0000;"> '+i+'</a></td>';
	}
	else
	{
	html += '<td><a href="#" onclick="getMyFriends1('+i+')"> '+i+'</a></td>';
	}
    } 	
	html += '</tr>';
	document.getElementById('dspFriends').innerHTML = html;

  }
  });
});
getUserFriends();
}



function getDiv(thisValue){
	debug("getDiv");
	
if(thisValue=='Event'){showFeed('div3','div2','div1','div4','div5','div6');}else if(thisValue=='Recipe'){showFeed('div1','div2','div3','div4','div5','div6');}else if(thisValue=='Announcement'){showFeed('div2','div3','div1','div4','div5','div6')}else if(thisValue=='Gift'){showFeed('div5','div3','div1','div4','div2','div6')}else if(thisValue=='Wall'){showFeed('div6','div3','div1','div4','div2','div5')}else{showFeed('div4','div3','div1','div5','div2','div6')}
}
function makeCachedRequest(url, callback, params, refreshInterval) {
var ts = new Date().getTime();
var sep = "?";
if (refreshInterval && refreshInterval > 0) {
ts = Math.floor(ts / (refreshInterval * 1000));
}
if (url.indexOf("?") > -1) {
sep = "&";
}
url = [ url, sep, "nocache=", ts ].join("");
gadgets.io.makeRequest(url, callback, params);
} 

function alterLocLink(appID){
	debug("alterLocLink");
	//alert(appID);
window.top.location=path+"?appId="+appID+"&up_chk=1&up_tab=tl_0_0";
}
function showfriendslist(){
	debug("showfriendslist");
	
	if(document.getElementById('friends').style.display=='block'){
		document.getElementById('friends').style.display='none'
	}else{
		document.getElementById('friends').style.display='block'
		showDiv1('friends','family','familyList','frdImg','famImg','familyImg',document.getElementById('strpath').value);
		//showDiv('familyList');
	};
}			
function showDiv1(divID1,divID2,divID3,imgID1,imgID2,imgID3,path,countP)
{
	debug("showDiv1");
	hideDiv(divID2);
	//hideDiv(divID3);
	if(imgID1=='famImg'){
		document.getElementById(imgID1).src=path+'/btn_family1.gif';
		//document.getElementById(imgID2).src=path+'/btn_friend.gif';
		document.getElementById(imgID3).src=path+'/btn_family_list.gif';
	}
	
	if(imgID1=='frdImg'){
		//document.getElementById(imgID1).src=path+'/btn_friend1.gif';
		//document.getElementById(imgID2).src=path+'/btn_family.gif';
		//document.getElementById(imgID3).src=path+'/btn_family_list.gif';
		getFriends(countP);
	}	
	
	if(imgID1=='familyImg'){
		document.getElementById(imgID1).src=path+'/btn_family_list1.gif';
		document.getElementById(imgID2).src=path+'/btn_family.gif';
		//document.getElementById(imgID3).src=path+'/btn_friend.gif';
		
	}
		
	X = document.getElementById(divID1);
	
	X.style.display = "block";
	
}
function showFeed(div1,div2,div3,div4,div5,div6){
	debug("showFeed");
	
	document.getElementById(div1).style.display='block';
	document.getElementById(div2).style.display='none';
	document.getElementById(div3).style.display='none';	
	document.getElementById(div4).style.display='none';	
	document.getElementById(div5).style.display='none';	
	document.getElementById(div6).style.display='none';	

} 
function saveData(sub){
		debug("saveData");
		//alert(sub);
		//sub=escape(sub);
		url=servrpath+"saveNews.php?Submit1="+sub;
	//alert('url='+url);
			var params = {};


			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;


			makeCachedRequest(url, function(responseText){
		document.getElementById('NewsDiv').innerHTML=responseText.text;


			},params,0) ;


}

function allFeed(sub,pn){
		debug("allFeed");
	
		url=servrpath+"allFeed.php?"+sub+"&_pn="+pn;
		
	var params = {};


			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;


			makeCachedRequest(url, function(responseText){
		
		document.getElementById('NewsDiv').innerHTML=responseText.text;

_IG_AdjustIFrameHeight();
			},params,0) ;


}	

function saveQuestion(t,q,a,u,apid){
debug("saveQuestion");

langvalue=document.getElementById('langv').value;
//alert('langv'+langv);
		if(t=='Announcement')
		{
		// Comment=document.getElementById('Comment');
		//alert(Comment);
		// if(Comment.value=="")
		// {
		// alert("Please enter comments");
		// Comment.focus();
		// return false;
		// }
				if(q=='Other')
				{
				What5=document.getElementById('What5');
				if(What5.value=="")
				{
			
				//alert(arrcomm[langv]+"please enter other det5555ails");
				//alert(arrcomm[langv]);
				alert(arrcomm[langvalue][0]);
				What5.focus();
				return false;
				}
				
				q=document.getElementById('What5').value;
				// document.getElementById('What5').value="";
				// document.getElementById('Comment').value="";
				}
				
				Comment=document.getElementById('Comment');
				//alert(Comment);
				if(Comment.value=="")
				{
				alert(arrcomm[langvalue][1]);
				//alert("Please enter comments");
				Comment.focus();
				return false;
				}
					

				document.getElementById('What5').value="";
				document.getElementById('Comment').value="";
			    sub="&What="+escape(q)+"&Comment="+escape(a)+"&profile_id="+u+"&apid="+apid;
		//alert(sub);
		}
		else if(t=='Question')
		{
		
			sub="&question="+escape(q)+"&answer="+escape(a)+"&profile_id="+u+"&apid="+apid;
			
			
		}
		else if(t=='Story')
		{
		story=document.getElementById('story');
		//alert(story);
		if(story.value=="")
		{
		alert(arrcomm[langvalue][2]);
		//alert('please enter family story');
		story.focus();
		return false;
		}
		document.getElementById('story').value="";
		
			sub="&Story="+escape(q)+"&profile_id="+u+"&apid="+apid;
		}
		else if(t=='Gift')
		{
		answer=document.getElementById('answer');
		if(answer.value=="")
		{
		alert(arrcomm[langvalue][3]);
		//alert('please enter gift details');
		answer.focus();
		return false;
		}
		
		    document.getElementById('answer').value="";	
			sub="&Gift="+escape(q)+"&Details="+escape(a)+"&profile_id="+u+"&apid="+apid;
		}else if(t=='Wall')
		{
			sub="&Wall="+escape(q)+"&Details="+escape(a)+"&profile_id="+u+"&apid="+apid;
		}
		
		saveData(sub);

return false;


}

function getCustomAnnouncement(what){
debug("getCustomAnnouncement");

if(what=='Other'){
	document.getElementById('divOther').style.display='block';
		document.getElementById('What').value="";
	
}else{
		document.getElementById('divOther').style.display='none';
		document.getElementById('What').value=what;
}


}
/*	
function saveRecipe(t,Recipe,Ingredients,Instructions,u,apid){
if(t=='Recipe')
{
document.getElementById('Recipe_Name').value="";	
document.getElementById('Ingredients').value="";	
document.getElementById('Instructions').value="";	
			sub="&Recipe="+escape(Recipe)+"&Ingredients="+escape(Ingredients)+"&Instructions="+escape(Instructions)+"&profile_id="+u+"&apid="+apid;
		}
		
		saveData(sub);
}	*/
function saveRecipe(t,Recipe,Ingredients,Instructions,u,apid){
	debug("saveRecipe");
	
	//alert('t: '+t);
	//alert('Recipe: '+Recipe);
	//alert('Ingredients: '+Ingredients);
	//alert('Instructions: '+Instructions);
	langvalue=document.getElementById('langv').value;
	
//alert(langv);
if(t=='Recipe')
{
	Recipe_Name=document.getElementById('Recipe_Name');
	Ingredients_Name=document.getElementById('Ingredients');
	Instructions_Name=document.getElementById('Instructions');
	if(Recipe_Name.value=="")
	{
	// alert("Please enter recipe name");
	// Recipe_Name.focus();
	// return false;
	}

	else if(Ingredients_Name.value=="")
	{
	// alert("Please enter ingredients");
	// Ingredients_Name.focus();
	// return false;
	}

	else if(Instructions_Name.value=="")
	{
	alert(arrcomm[langvalue][4]);
	//alert("Please enter instructions");
	Instructions_Name.focus();
	return false;
	}
	else
	{    
		sub="&Recipe="+escape(Recipe)+"&Ingredients="+escape(Ingredients)+"&Instructions="+escape(Instructions)+"&profile_id="+u+"&apid="+apid;
		//alert(sub);
		saveData(sub);
		document.getElementById('Recipe_Name').value="";	
		document.getElementById('Ingredients').value="";	
		document.getElementById('Instructions').value="";
		return true;
	}	
}
//saveData(sub);		
		
}	

function deleteFeed(id,u,apid){
		debug("deleteFeed");
		
		sub="&delete="+id+"&profile_id="+u+"&apid="+apid;
		
		saveData(sub);
}

function deleteFeedPage(id,u,apid,pn){
		debug("deleteFeedPage");
		
		sub="&delete="+id+"&profile_id="+u+"&apid="+apid;
		
		allFeed(sub,pn);

}
/*
function saveEvent(Name,StartDate,StartDateHour,StartDateMinute,EndDate,EndDateHour,EndDateMinute,loc,address,u,apid){
document.getElementById('Name').value="";
document.getElementById('txt_startdate1').value=""; 
//documentgetElementById('StartDateHour').value="";
//documentgetElementById('StartDateMinute').value=""; 
document.getElementById('txt_enddate1').value="";
//documentgetElementById('EndDateHour').value=""; 
//documentgetElementById('EndDateMinute').value="";
document.getElementById('loc').value="";
document.getElementById('address').value="";

sub="&Name="+escape(Name)+"&StartDate="+StartDate+"&StartDateHour="+StartDateHour+"&StartDateMinute="+StartDateMinute+"&EndDate="+EndDate+"&EndDateHour="+EndDateHour+"&EndDateMinute="+EndDateMinute+"&loc="+escape(loc)+"&address="+escape(address)+"&profile_id="+u+"&apid="+apid;

		
		saveData(sub);

return false;


}
*/

function saveEvent(Name,StartDate,StartDateHour,StartDateMinute,EndDate,EndDateHour,EndDateMinute,loc,address,u,apid){
debug("saveEvent");

langvalue=document.getElementById('langv').value;
//alert(langv);
newname=document.getElementById('Nameevent');
newloc=document.getElementById('loc');
newadd=document.getElementById('address');
newstartdate=document.getElementById('');
neweenddate=document.getElementById('');
if(newname.value=="")
	{
	alert(arrcomm[langvalue][5]);
	//alert("please enter name");
	newname.focus();
	return false;
	}
	else if(newloc.value=="")
	{
	alert(arrcomm[langvalue][6]);
	//alert("Please enter location");
	newloc.focus();
	return false;
	}
	
	else if(newadd.value=="")
	{
	alert(arrcomm[langvalue][7]);
	//alert("Please enter address");
	newadd.focus();
	return false;
	}
else
{

sub="&Name="+escape(newname.value)+"&StartDate="+StartDate+"&StartDateHour="+StartDateHour+"&StartDateMinute="+StartDateMinute+"&EndDate="+EndDate+"&EndDateHour="+EndDateHour+"&EndDateMinute="+EndDateMinute+"&loc="+escape(newloc.value)+"&address="+escape(newadd.value)+"&profile_id="+u+"&apid="+apid;
saveData(sub);
document.getElementById('Nameevent').value="";
document.getElementById('txt_startdate1').value=""; 
document.getElementById('txt_enddate1').value="";
document.getElementById('loc').value="";
document.getElementById('address').value="";

return true;
}
}


function saveComment(dataComment,id,u,apid){
//alert('444');

	debug("saveComment");
	sub="&replyComment="+escape(dataComment)+"&id="+id+"&profile_id="+u+"&apid="+apid+"";
		//alert(sub);
		saveData(sub);

}
function saveCommentPage(dataComment,id,u,apid,pn){

	debug("saveCommentPage");

	sub="&replyComment="+escape(dataComment)+"&id="+id+"&profile_id="+u+"&apid="+apid;
		
		allFeed(sub,pn);

}


function getFriends(countP){
	debug("getFriends");
	
	var req = opensocial.newDataRequest();
	var opt_params = { };
        opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
          opensocial.Person.Field.PROFILE_URL
        ];

    req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_params), "viewerFriends");
	//req.add(req.newFetchPeopleRequest ('VIEWER_FRIENDS'), 'viewerFriends');
	req.send(function(dataResponse){
		var html;
	var j=0;
	html = '<table width="100%" cellspacing="2" cellpadding="2" align="center" border="0"><tr><td colspan="3" style="height: 15px;"></td></tr>';
	var viewerFriends = dataResponse.get('viewerFriends').getData();
	countP=document.getElementById('countProfile').value;
	
	chkPos=document.getElementById('chkPos').value;
	show="none";
	
	 if(countP<6){
	
		if(chkPos<1 || chkPos > 6){
			show="block";
		}else{
		show="none";
		}
	}
	
	
	//alert(viewerFriends);
	if(viewerFriends){
	viewerFriends.each(function(person) {
profile=_args()["parent"] +  person.getField(opensocial.Person.Field.PROFILE_URL);
		//alert(profile);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		//alert(img1+" "+arrImage[numPic-1]);
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		
		if(j%3==0 && j!=0){
		html += '</tr><tr>';
		}
		// var nmm=person.getDisplayName();
		// alert(nmm);
		html += '<td valign="top"><table align="center" width="110"><tr><td valign="top" align="center"	onClick="if(document.getElementById(\'personInfo'+j+'\').style.display==\'block\'){document.getElementById(\'personInfo'+j+'\').style.display=\'none\' }else{document.getElementById(\'personInfo'+j+'\').style.display=\'block\'};"><img src="'+userPic+'" width="50px" height="50px" border="0" style="padding: 10px; cursor: pointer; border: 5px solid #52a25e;" ></tr><tr><td style="font-size: 11px; text-decoration: none; color: #4864A0; text-align: center; height: 22px;">'+ person.getDisplayName() +'</td></tr>';
		html += '<tr><td><div id="personInfo'+j+'" style="display:none"><table cellspacing="2" cellpadding="2" border="0" width="100%"><tr><td class="field loc" style="font-family:verdana;font-size:10px;text-align: left;"><div style="font-family:verdana;font-size:10px; display:'+show+'"><input style="font-size:11px;" type="checkbox" name="chkToProfile'+j+'" id="chkToProfile'+j+'" value="1" onclick="dispRelated(\''+j+'\');"> Add To Profile</div></td></tr><tr><td style="font-family:verdana;font-size:10px;" class="field" ><div id="related'+j+'" style="display:none" >		<b>Relation:</b><input type="text"  size="6" name="relatedF'+j+'" id="relatedF'+j+'" style="height: 25px; border: 1px solid #cccccc; background: #fff url(../images/field_bg.gif) repeat-x; padding: 2px 2px 2px 2px; Background-color: #F1F1F1;font-size: 10px; color: #847E5F;" size="20" /></div></td></tr>';
		html += '<tr><td width="100%" style="padding-left:20px; text-align:center;" height="28px"><input type="button" name="btnAdd" id="btnAdd" value="Add" class="btn" onClick="addAsMember(\''+person.getId()+'\',\''+person.getDisplayName()+'\',\''+imag+'\',document.getElementById(\'related'+j+'\').value,'+j+',\''+person.getField(opensocial.Person.Field.PROFILE_URL)+'\')"></td></tr></table></div></td></tr></table></div></td>';
		//html += '</tr><tr><td height="2" colspan="6"></td></tr></table></td>';
		
		j++;
	});
	html += '</tr>';
	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table>';
	//alert(html);dspFriends
	document.getElementById('dspFriends').innerHTML = html;
	gadgets.window.adjustHeight('2220');
	
	
	
	
	});
}




function getMyFriends(){
	debug("getMyFriends");
	
	X = document.getElementById('msgShw');
	X.style.display = "block";
	var req = opensocial.newDataRequest();
	var opt_params = { };
	opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
	req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER, opt_params), "viewer");
	req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_params), "viewerFriends");
	req.send(getAllFriends);
}






function fetchPeopleHandler2(resp) {
	debug("fetchPeopleHandler2");
	
  if (!resp.hadError()) { // always verify whether your request was successful
    var viewerFriends = resp.get('viewerFriends').getData(); // use the key passed with the request to "get" the appropriate data

    var html;
	var j=0;
	html = '<table width="65%" cellspacing="2" cellpadding="2" align="center" border="0">';
	//var viewerFriends = dataResponse.get('viewerFriends').getData();
	if(viewerFriends){
	viewerFriends.each(function(person) {
profile= person.getField(opensocial.Person.Field.PROFILE_URL);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		
		html += '<tr><td style="height: 15px;"></td></tr><tr><td align="center"><div id="div'+j+'" style="background: #ffffff; border: 1px #dddddd solid ; cursor: pointer;" onclick="this.id.background=\'#769DD0\';addtofamilylist(\''+person.getId()+'\',\''+person.getDisplayName()+'\',\''+userPic+'\',\''+person.getField(opensocial.Person.Field.PROFILE_URL)+'\');"><table border="0" width="100%"><tr><td width="30%" style="text-align: center; " align="center"><img src="'+userPic+'" width="50px" height="50px" border="0"></td><td width="70%" style="text-align: left; font-family:tahoma;font-size:11px;color:#1E3A73; height:12px;">'+ person.getDisplayName() +'</td></tr></table></div></td></tr>';		
		j++;
	});
	
	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table>';
	document.getElementById('dspFriends').innerHTML = html;

  }
};
////////

///////


function getMyFriends111(){
debug("getMyFriends111");
	
var req = opensocial.newDataRequest();

req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
req.add(req.newFetchPeopleRequest ('VIEWER_FRIENDS'), 'viewerFriends');

req.send(function(data){
var console = {};
console.log = function(msg) {};
person = data.get('viewer').getData();
my_data = data.get('viewerFriends');
my_data = my_data.getData() || {};
var totalFriendCount = data.get('viewerFriends').getData().getTotalSize();
//alert(totalFriendCount);



	var params = {};
params[opensocial.DataRequest.PeopleRequestFields.MAX] = totalFriendCount;
//params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;
params[opensocial.DataRequest.PeopleRequestFields.SORT_ORDER] = opensocial.DataRequest.SortOrder.NAME;

var req = opensocial.newDataRequest();
req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS, params), 'viewerFriends');
req.send(fetchPeopleHandler);
});

}

function showDiv(divID)
{
	debug("showDiv");
	X = document.getElementById(divID);
	X.style.display = "block";
}

function displayFriends(dataResponse){
	debug("displayFriends");
	//alert("here");
	var html;
	var j=0;
	html = '<table width="100%" cellspacing="2" cellpadding="2" align="center" border="0">';
	var viewerFriends = dataResponse.get('viewerFriends').getData();
	//alert(viewerFriends);
	if(viewerFriends){
	viewerFriends.each(function(person) {
profile=_args()["parent"] +  person.getField(opensocial.Person.Field.PROFILE_URL);
		//alert(profile);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		//alert(img1+" "+arrImage[numPic-1]);
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}

		//alert(userPic);
		html += '<tr><td style="height: 15px;"></td></tr><tr><td align="center"><a href="'+profile+'" ><img src="'+userPic+'" width="50px" height="50px" border="0" ></a></td><td style="font-family:verdana;font-size:12px;">'+ person.getDisplayName() +'</td>';
		html += '<td align="center" style="font-family:verdana;font-size:12px;" class="field"><div id="personInfo"><b>Email:</b><input type="text" value="Optional" onfocus="cleartext(\'txtEmail'+j+'\')" size="10"name="txtEmail'+j+'" id="txtEmail'+j+'" style="height: 20px; border: 1px solid #cccccc; background: #fff url(../images/field_bg.gif) repeat-x; padding: 2px 2px 2px 2px; Background-color: #F1F1F1;font-size: 10px; color: #847E5F;" size="20"></td>';
		html += '<td align="center"><input type="button" name="btnAdd" id="btnAdd" value="Add" class="btn" onClick="addAsMember(\''+person.getId()+'\',\''+person.getDisplayName()+'\',\''+imag+'\',document.frm.txtEmail'+j+'.value,'+j+',\''+person.getField(opensocial.Person.Field.PROFILE_URL)+'\')"></div></td>';
		html += '</tr><tr><td height="2" colspan="6"></td></tr>';
		
		j++;
	});
	html += '<tr><td align="center" colspan="6"><div align="center" style="position: relative; left: 200px;"><input type="button" class="btn" value="Cancel" onclick="alterLoc();"></div></td></tr>';
	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table>';
	document.getElementById('dspFriends').innerHTML = html;
}

function getAllFriends(dataResponse){
	debug("getAllFriends");
	
	//alert("here");
	var html;
	var j=0;
	var viewer = dataResponse.get("viewer").getData();
        var name = viewer.getDisplayName();
		
	html = '<table width="65%" cellspacing="2" cellpadding="2" align="center" border="0">';
	var viewerFriends = dataResponse.get('viewerFriends').getData();
	
	if(viewerFriends){
	viewerFriends.each(function(person) {
profile= person.getField(opensocial.Person.Field.PROFILE_URL);
		//alert(profile);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		//alert(img1+" "+arrImage[numPic-1]);
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		//alert(userPic);
		//html += '<tr><td style="height: 15px;"></td></tr><tr><td align="center"><div id="div'+j+'" style="background: #ffffff" onclick="this.id.background=\'#769DD0\';addtofamilylist(\''+person.getId()+'\',\''+person.getDisplayName()+'\');"><table><tr><td><img src="'+userPic+'" width="50px" height="50px" border="0"></td><td style="font-family:verdana;font-size:12px;">'+ person.getDisplayName() +'</td></tr></table></div></td></tr>';
		html += '<tr><td style="height: 5px;"></td></tr><tr><td align="center"><div id="div'+j+'" style="background: #ffffff; border: 1px #dddddd solid ; cursor: pointer;" onclick="this.id.background=\'#769DD0\';addtofamilylist(\''+person.getId()+'\',\''+person.getDisplayName()+'\',\''+userPic+'\',\''+person.getField(opensocial.Person.Field.PROFILE_URL)+'\');"><table border="0" width="100%"><tr><td width="30%" style="text-align: center; " align="center"><img src="'+userPic+'" width="50px" height="50px" border="0"></td><td width="70%" style="text-align: left; font-family:tahoma;font-size:11px;color:#1E3A73; height:12px;">'+ person.getDisplayName() +'</td></tr></table></div></td></tr>';		
		j++;
	});
	
	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table>';
	document.getElementById('dspFriends').innerHTML = html;
}

function hideDiv(divID)
{
	debug("hideDiv");
	X = document.getElementById(divID);
	X.style.display = "none";
}

function hideEmailDiv(divID)
{
	debug("hideEmailDiv");
	X = document.getElementById(divID);
	if(X.style.display == "block") {
		X.style.display = "none";
	} else {
		X.style.display = "block";
	}	
}

function display(val)
{
	debug("display");
	X = document.getElementById('divUpload');
	Y = document.getElementById('divProduct');
	if(val =="browser")
	{
		X.style.display = "block";
		Y.style.display = "none";
		document.getElementById('hdnImage').value="";
		Z = document.getElementById('divPic');
		Z.style.display = "none";
	}
	else if(val == "album")
	{
		X.style.display = "none";
		Y.style.display = "block";
	}
}
function displayDiv(elem,pictureID)
{
	debug("displayDiv");
	X = document.getElementById('divProduct');
	Y = document.getElementById('divPic');
	X.style.display = "none";
	Y.style.display = "block";
	document.getElementById('image1').src=elem;
	document.getElementById('hdnImage').value=elem;
	document.getElementById('hdnPictureID').value=pictureID;
	
}

		

function addAsMember(fid,name,image,email,id,purl)
{
	debug("addAsMember");
	var agree=confirm("Add this Orkut friend to your Family Tree?");
	if (agree)
	{
		document.frm.hdnText.value = "add";
		document.frm.hdnID.value=fid;
		document.frm.hdnName.value=name;
		document.frm.hdnImage.value=image;
		document.frm.hdnEmail.value=email;
		document.frm.hdnPurl.value=purl;
		Relation=document.getElementById('relatedF'+id).value;
		
		document.frm.relatedas.value=Relation;
		
		//requestSendMessage(fid);
		//alert("Notification Sended");
	}
	else
	{
		document.frm.hdnText.value = "notAdd";
		
	}
	document.frm.submit();
	
}
function submitForm(fname,lname,sex,image,email,oid,purl){
	debug("submitForm");

	var agree=confirm("Add this relative to your Family Tree?");
	if (agree)
	{
		document.frmFamLst.hdnSubmit.value = "submit";
		document.frmFamLst.hdnFname.value=fname;
		document.frmFamLst.hdnLname.value=lname;
		document.frmFamLst.hdnGender.value=sex;
		document.frmFamLst.hdnPicture.value=image;
		document.frmFamLst.hdnEmail.value=email;
		document.frmFamLst.hdnOrkutID.value=oid;
		document.frmFamLst.hdnPurl.value=purl;
		document.frmFamLst.hdnName.value=fname+" "+lname;
		
	}
	else
	{
		document.frmFamLst.hdnText.value = "notsubmit";
		
	}
	document.frmFamLst.submit();
}

function addtofamilylist(fid,name,image,purl)
{
	debug("addtofamilylist");
	var agree=confirm("Add this Orkut friend to your Family List?");
	if (agree)
	{
		document.frm.hdnText.value = "add";
		document.frm.hdnID.value=fid;
		document.frm.hdnName.value=name;
		document.frm.hdnImage.value=image;
		document.frm.hdnPurl.value=purl;
		document.frm.submit();
	}
	else
	{
		document.frm.hdnText.value = "notAdd";
		return false;
	}
	
}

function validate()
{
	debug("validate");
	var fname = document.FormAddPerson.fname;
	var lname = document.FormAddPerson.lname;
	var email = document.FormAddPerson.email;
	var ddate = document.FormAddPerson.ddate;
	var chkDec = document.FormAddPerson.chkDec;
	
	if(fname.value=="")
	{
		alert("Please enter first name");
		fname.focus();
		return false;
	}
	
	if(lname.value=="")
	{
		alert("Please enter last name");
		lname.focus();
		return false;
	}
	
	
	if(document.FormAddPerson.chkDec.checked != true)
	{
		/*if(email.value=="")
		{
			alert("Please enter email address");
			email.focus();
			return false;
		}
		
		var reEmail=/^[0-9a-zA-Z_\.-]+\@[0-9a-zA-Z_\-]+\.[0-9a-zA-Z_\.-]*$/
		var reEDUEmail=/^[0-9a-zA-Z_\.-]+\@[0-9a-zA-Z_\-]+\.edu$/
		if(!reEmail.test(email.value))
		{
			alert("Please enter a valid email address.");
				email.value = "";
				email.focus();
				return false;
		}*/
	}
	
	return true;
}



function dispRelated(id){
	debug("dispRelated");
	
	rel='related'+id;
	chkpro='chkToProfile'+id;
	if(document.getElementById(chkpro).checked==true){
		document.getElementById(rel).style.display='block';
		document.getElementById('chkToProfileL').value=1;
	}else{
		document.getElementById(rel).style.display='none';
		document.getElementById('chkToProfileL').value=0;
		}
}

function cleartext(id){
	debug("cleartext");
document.getElementById(id).value='';
}



function writeActivity(txt) {
	debug("writeActivity");
	//alert("hello");
	
  var streamFolder = 'newsfeed';
  var streamTitle = 'News Feed';
  /*var stream_params = { };
  var stream = opensocial.newStream(streamFolder, streamTitle, stream_params);
  var title = [ txt,new Date().formatDate("l, M d") ].join("");
  var activity_params = {}
  var activity = opensocial.newActivity(stream, title, activity_params)
  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, response);*/
  $arrAct=txt.split("|");
  //alert("iam activity");
  //alert($arrAct[0]);
  var par = {};
  par[opensocial.Activity.Field.TITLE] = $arrAct[0];
  // + " at " + (new Date()).toString();
  par[opensocial.Activity.Field.BODY] = [ $arrAct[1],new Date().formatDate("l, M d") ].join("");
  //alert("new text k");
  var activity = opensocial.newActivity(par);
	opensocial.requestCreateActivity(activity,opensocial.CreateActivityPriority.HIGH,responseAct);
	
  gadgets.window.adjustHeight();
}

function responseAct() {
alert("Your Relative Has Been Added");
 }
function response() {
	debug("response corpo nullo");
 }
function getActivities() {
	debug("getActivities");
	
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.add(req.newFetchActivitiesRequest('VIEWER'), 'viewerActivities');
  req.add(req.newFetchActivitiesRequest('VIEWER_FRIENDS'), 'friendActivities');
  req.send(showActivities);
}

function showActivities(dataResponse) {
	debug("showActivities");
	
  var viewer = dataResponse.get('viewer').getData();
  var viewerActivities = dataResponse.get('viewerActivities').getData()['activities'];
  var friendActivities = dataResponse.get('friendActivities').getData()['activities'];
  _gel('activity').innerHTML ="";
  var htmlout = '';
  htmlout += '<h2>Your activities:</h2><br>';
  htmlout += getActivitiesHtml(viewerActivities);
  htmlout += '<h2>Your friends\' activities:</h2><br>';
  htmlout += getActivitiesHtml(friendActivities);
  _gel('activity').innerHTML = htmlout;
}

function getActivitiesHtml(stream) {
	debug("getActivitiesHtml");
  console.log(stream);
  var htmlout = '';
  stream.each(function(activity) {
      htmlout += activity.getField('title');
      htmlout += '<br>';
  });
  return htmlout;
}

function alterLoc(){
	debug("alterLoc");
	
	appID=document.frm.hdnApID.value;
	//alert(appID);
window.top.location=path+"?appId="+appID+"&up_chk=1&up_tab=tl_0_0";
}
function linkToOrkut(dataResponse){
	debug("linkToOrkut");
	
var html;
	var j=0;
	html = '<table width="100%" cellspacing="2" cellpadding="2" align="center" border="0">';
	var viewerFriends = dataResponse.get('viewerFriends').getData();
	//alert(viewerFriends);
	if(viewerFriends){
	viewerFriends.each(function(person) {
		profile=_args()["parent"] +  person.getField(opensocial.Person.Field.PROFILE_URL);
		//alert(profile);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		//alert(img1+" "+arrImage[numPic-1]);
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		//alert(userPic);
		html += '<tr><td style="height: 15px;"></td></tr><tr><td align="center"><input type="radio" name="rdfriend" value="'+person.getId()+'"></td><td style="font-family:verdana;font-size:12px;">'+person.getDisplayName() +'</td>';
		
	
		html += '</tr><tr><td height="2" colspan="2"></td></tr>';
			html += '<tr><input type="hidden" name="image'+person.getId()+'" id="image'+person.getId()+'" value="'+imag+'"><input type="hidden" name="name'+person.getId()+'" id="name'+person.getId()+'" value="'+person.getDisplayName()+'"><input type="hidden" name="url'+person.getId()+'" id="url'+person.getId()+'" value="'+person.getField(opensocial.Person.Field.PROFILE_URL)+'"><td align="center" colspan="6"><div align="center" style="position: relative; left: 200px;"></div></td></tr>';
		j++;
	});

	}else{
		html += '<tr><td style="font-family:verdana;font-size:12px;">Sorry, No friends found</td></tr>';
	}
	html += '</table>';
	document.getElementById('dspLinkFriends').innerHTML = html;

}

function getFriendList(){
	debug("getFriendList");
	
	var req = opensocial.newDataRequest();
	req.add(req.newFetchPeopleRequest ('VIEWER_FRIENDS'), 'viewerFriends');
	req.send(linkToOrkut);
}
function confirmLink(frm){
	debug("confirmLink");
	
    var cnt = -1;
	
	//alert(document.FormEditPerson.rdfriend.length);
  for (counter = 0; counter < document.FormEditPerson.rdfriend.length; counter++)
{

if (document.FormEditPerson.rdfriend[counter].checked)
cnt = counter; 
//alert(document.FormEditPerson.rdfriend[counter].checked);
}
	//alert(cnt);
    if (cnt > -1) 
		return true;
	else{
		alert("Please select friend");
		return false;
		}
		
}



function getUserFriends(){
	debug("getUserFriends");
	
	//alert("jj"+userID);
	var req = opensocial.newDataRequest();
	var opt_params = { };
	opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
	req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER, opt_params), "viewer");
	req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS, opt_params), "viewerFriends");
	req.send(getUserAllFriends);
}

function getUserAllFriends(dataResponse){
	debug("getUserAllFriends");
	
	friendId = '{ "friend":[';
	var j=0;
	var viewer = dataResponse.get("viewer").getData();
	var profile_id=viewer.getId();
	//alert(profile_id);
        var name = viewer.getDisplayName();
		

	var viewerFriends = dataResponse.get('viewerFriends').getData();
	var totalFriendCount = dataResponse.get('viewerFriends').getData().getTotalSize();
//alert("dd"+totalFriendCount);
	if(viewerFriends){
viewerFriends.each(function(person) {

	
profile= person.getField(opensocial.Person.Field.PROFILE_URL);
		//alert(profile);
		img1=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
		arrImage=img1.split("/");
		numPic=arrImage.length;
		//alert(img1+" "+arrImage[numPic-1]);
		if(arrImage[numPic-1]=="null")
		{
			userPic=servrpath+"images/male32_root.gif";
			imag="";
		}
		else
		{
			userPic=person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			imag=userPic;
		}
		
//alert(person.getField(opensocial.Person.Field.STATUS));

		if(j>0){
		friendId+=',';
		}
		friendId+='{"name": "'+person.getDisplayName()+'", "id": "'+person.getId()+'", "url": "'+person.getField(opensocial.Person.Field.PROFILE_URL)+'", "image": "'+person.getField(opensocial.Person.Field.THUMBNAIL_URL)+'", "status": "'+person.getField(opensocial.Person.Field.STATUS)+'"}';
		// friendId+=;
		// friendId+=person.getField(opensocial.Person.Field.PROFILE_URL);
			
		j++;
	});
	friendId+='] }';
	
	}	
	//alert(friendId);
//	alert(servrpath);
	//window.location.href = servrpath+"treeShared.php?friendId="+friendId;
	
	//url=servrpath+"treeShared.php?friendId="+escape(friendId);
	//alert(url);
			var params = {};
 var postdata = {
    data1 : friendId
   
  };
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
  var url = servrpath+"treeShared.php?userID="+profile_id;
 // gadgets.io.makeRequest(url, response, params);
			//params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;


			makeCachedRequest(url, function(responseText){
		//alert(responseText.text);


			},params,0) ;

	return friendId;
};